package cn.mumzone.pos.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Field;

import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.SPManager;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.PopupHintEntity;

/**
 * Created by JoeLjt on 2019/11/20.
 * Email: lijiateng1219@gmail.com
 * Description:
 */
public class PopupUtil {

    private static final int OFFSET_POPUP_DISPLAY = 4;
    private static final int OFFSET_ADJUST_ARROW_POSITION = 25;

    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int LEFT_TOP = 3;
    public static final int LEFT_BOTTOM = 4;
    public static final int RIGHT_TOP = 5;
    public static final int RIGHT_BOTTOM = 6;

    public static final int CASH_OPEN_ORDER = 1001;
    public static final int CASH_RESERVE = 1002;
    public static final int CASH_REFRESH = 1003;

    public static final int TIME_TABLE_REFRESH = 2001;

    public static final int MEMBER_ADD_NEW = 3001;

    public static final int OPEN_ORDER_ADD_MEMBER = 4001;
    public static final int OPEN_ORDER_UNBIND_MEMBER = 4002;
    public static final int OPEN_ORDER_MODIFY_PRICE = 4003;
    public static final int OPEN_ORDER_MODIFY_SERVER = 4004;

    public static final int RESERVE_MAKE_ORDER = 5001;

    public static final int SETTLE_ADD_MEMBER = 6001;
    public static final int SETTLE_UNBIND_MEMBER = 6002;
    public static final int SETTLE_RECALL_DISCOUNT = 6003;
    public static final int SETTLE_ERASE_ZERO = 6004;

    public static PopupHintEntity hintEntity
            = JSON.parseObject(AppConstants.USER_POPUP_HINT, PopupHintEntity.class);

    public static void dismissPopup(int popupCode, PopupWindow popupWindow) {

        if (popupWindow != null && popupWindow.isShowing()) {
            try {
                Field targetField = PopupHintEntity.class
                        .getDeclaredField(getPopupHintFieldByCode(popupCode));
                targetField.setAccessible(true);
                targetField.setBoolean(hintEntity, true);

                SPManager.getInstance().put(AppConstants.TEMP_SELLER_ID, JSON.toJSONString(hintEntity));
                popupWindow.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static PopupWindow showBlackBgPopup(final View anchor, int popupCode, String content, int dropPosition) {
        return setupPopupWithEntity(anchor, popupCode, content, dropPosition, true);
    }

    public static PopupWindow showWhiteBgPopup(final View anchor, int popupCode, String content, int dropPosition) {
        return setupPopupWithEntity(anchor, popupCode, content, dropPosition, false);
    }

    private static PopupWindow setupPopupWithEntity(final View anchor, int popupCode, String content, int dropPosition, boolean isBlack) {

        if (AppConstants.ALL_HINT_DISMISSED) {
            return null;
        }

        try {

            Field targetField = PopupHintEntity.class
                    .getDeclaredField(getPopupHintFieldByCode(popupCode));
            targetField.setAccessible(true);

            if (targetField.getBoolean(hintEntity)) {
                return null;
            }

            final PopupWindow popupWindow = initPopup(anchor, content, isBlack, dropPosition);
            final Field finalTargetField = targetField;
            popupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        finalTargetField.setBoolean(hintEntity, true);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }

                    SPManager.getInstance().put(AppConstants.TEMP_SELLER_ID, JSON.toJSONString(hintEntity));
                    popupWindow.dismiss();
                }
            });
            return popupWindow;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static PopupWindow initPopup(View anchor, String content, boolean isBlack, int dropPosition) {

        Context context = anchor.getContext();

        View contentView = LayoutInflater.from(context).inflate(R.layout.popup_base_hint, null);
        final PopupWindow mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);

        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        contentView.setBackground(getPopupBackground(anchor.getContext(), isBlack, dropPosition));

        AppCompatImageView ivPopHint = contentView.findViewById(R.id.iv_popup);
        ivPopHint.setImageResource(isBlack ? R.drawable.svg_ic_pop_up : R.drawable.svg_ic_pop_up_white_bg);

        TextView tvContent = contentView.findViewById(R.id.tv_popup_toast);
        tvContent.setTextColor(isBlack ? Color.parseColor("#999999") : Color.parseColor("#535353"));
        tvContent.setText(content);
        adjustPopupGravity(context, contentView, dropPosition);

        AppCompatImageView ivPopDismiss = contentView.findViewById(R.id.iv_dismiss);
        ivPopDismiss.setImageResource(isBlack ? R.drawable.svg_ic_close_popup : R.drawable.svg_ic_close_popup_white_bg);

        mPopWindow.setOutsideTouchable(false);
        mPopWindow.setTouchable(true);

        adjustPopupLocation(mPopWindow, contentView, anchor, dropPosition);

        return mPopWindow;
    }

    private static void adjustPopupGravity(Context context, View contentView, int dropPosition) {

        int paddingNormal = DisplayUtil.dip2px(context, 10);
        int paddingForArrowSide = DisplayUtil.dip2px(context, 18);

        switch (dropPosition) {
            case LEFT:
                contentView.setPadding(paddingNormal, paddingNormal, paddingForArrowSide, paddingNormal);
                break;
            case RIGHT:
                contentView.setPadding(paddingForArrowSide, paddingNormal, paddingNormal, paddingNormal);
                break;
            case LEFT_TOP:
            case RIGHT_TOP:
                contentView.setPadding(paddingNormal, paddingNormal, paddingNormal, paddingForArrowSide);
                break;
            case LEFT_BOTTOM:
            case RIGHT_BOTTOM:
                contentView.setPadding(paddingNormal, paddingForArrowSide, paddingNormal, paddingNormal);
                break;
        }

        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
        contentView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    private static void adjustPopupLocation(PopupWindow popupWindow, View contentView, View anchor, int dropPosition) {
        switch (dropPosition) {
            case LEFT:
                popupWindow.showAsDropDown(anchor, -contentView.getMeasuredWidth() - OFFSET_POPUP_DISPLAY,
                        -anchor.getMeasuredHeight() / 2 - contentView.getMeasuredHeight() / 2);
                break;
            case RIGHT:
                popupWindow.showAsDropDown(anchor,
                        anchor.getMeasuredWidth() + OFFSET_POPUP_DISPLAY,
                        -contentView.getMeasuredHeight());
                break;
            case LEFT_TOP:
                popupWindow.showAsDropDown(anchor,
                        -contentView.getMeasuredWidth() + anchor.getMeasuredWidth() / 2 + OFFSET_ADJUST_ARROW_POSITION,
                        -contentView.getMeasuredHeight() - anchor.getMeasuredHeight() - OFFSET_POPUP_DISPLAY);
                break;
            case LEFT_BOTTOM:
                popupWindow.showAsDropDown(anchor,
                        -contentView.getMeasuredWidth() + anchor.getMeasuredWidth() / 2 + OFFSET_ADJUST_ARROW_POSITION,
                        OFFSET_POPUP_DISPLAY);
                break;
            case RIGHT_TOP:
                popupWindow.showAsDropDown(anchor,
                        anchor.getMeasuredWidth() / 2 - OFFSET_ADJUST_ARROW_POSITION,
                        -contentView.getMeasuredHeight() - anchor.getMeasuredHeight() - OFFSET_POPUP_DISPLAY);
                break;
            case RIGHT_BOTTOM:
                popupWindow.showAsDropDown(anchor,
                        anchor.getMeasuredWidth() / 2 - OFFSET_ADJUST_ARROW_POSITION,
                        OFFSET_POPUP_DISPLAY);
                break;
        }
    }

    private static Drawable getPopupBackground(Context context, boolean isBlack, int dropPosition) {

        if (isBlack) {
            switch (dropPosition) {
                case LEFT:
                    return context.getResources().getDrawable(R.drawable.popup_black_right);
                case RIGHT:
                    return context.getResources().getDrawable(R.drawable.popup_black_left);
                case LEFT_TOP:
                    return context.getResources().getDrawable(R.drawable.popup_black_right_bottom);
                case LEFT_BOTTOM:
                    return context.getResources().getDrawable(R.drawable.popup_black_right_top);
                case RIGHT_TOP:
                    return context.getResources().getDrawable(R.drawable.popup_black_left_bottom);
                case RIGHT_BOTTOM:
                    return context.getResources().getDrawable(R.drawable.popup_black_left_top);
            }
        } else {
            switch (dropPosition) {
                case LEFT:
                    return context.getResources().getDrawable(R.drawable.popup_white_right);
                case RIGHT:
                    return context.getResources().getDrawable(R.drawable.popup_white_left);
                case LEFT_TOP:
                    return context.getResources().getDrawable(R.drawable.popup_white_right_bottom);
                case LEFT_BOTTOM:
                    return context.getResources().getDrawable(R.drawable.popup_white_right_top);
                case RIGHT_TOP:
                    return context.getResources().getDrawable(R.drawable.popup_white_left_bottom);
                case RIGHT_BOTTOM:
                    return context.getResources().getDrawable(R.drawable.popup_white_left_top);
            }
        }
        return null;
    }

    private static String getPopupHintFieldByCode(int popupCode) {
        switch (popupCode) {
            case CASH_OPEN_ORDER:
                return PopupHintEntity.getCashOpenOrder();
            case CASH_RESERVE:
                return PopupHintEntity.getCashReserve();
            case CASH_REFRESH:
                return PopupHintEntity.getCashRefresh();
            case MEMBER_ADD_NEW:
                return PopupHintEntity.getAddNewMember();
            case TIME_TABLE_REFRESH:
                return PopupHintEntity.getTimeTableRefresh();
            case OPEN_ORDER_ADD_MEMBER:
                return PopupHintEntity.getOpenOrderAddMember();
            case OPEN_ORDER_UNBIND_MEMBER:
                return PopupHintEntity.getOpenOrderUnBindMember();
            case OPEN_ORDER_MODIFY_PRICE:
                return PopupHintEntity.getOpenOrderModifyPrice();
            case OPEN_ORDER_MODIFY_SERVER:
                return PopupHintEntity.getOpenOrderModifyServer();
            case RESERVE_MAKE_ORDER:
                return PopupHintEntity.getReserveMakeOrder();
            case SETTLE_ADD_MEMBER:
                return PopupHintEntity.getSettleAddMember();
            case SETTLE_UNBIND_MEMBER:
                return PopupHintEntity.getSettleUnBindMember();
            case SETTLE_RECALL_DISCOUNT:
                return PopupHintEntity.getSettleRecallDiscount();
            case SETTLE_ERASE_ZERO:
                return PopupHintEntity.getSettleEraseZero();
        }
        return null;
    }

}
